import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  Input,
  Upload,
  Space,
  Select,
  message
} from 'antd'

import { PlusOutlined } from '@ant-design/icons'
import { Link } from 'react-router-dom'
import ReactQuill from 'react-quill'
import 'react-quill/dist/quill.snow.css'
import './index.scss'
import { useStore } from '@/store'
import { observer } from 'mobx-react-lite'
import { useState } from 'react'
import { http } from '@/utils'
const { Option } = Select

const Publish = () => {
  const { channelStore } = useStore()
  //存放上传图片的列表
  const [fileList, setFileList] = useState([])
  const onUploadChange = ({ fileList }) => {
    setFileList(fileList)
  }
  //切换图片
  const [imgcCount, setimgCount] = useState(1)
  const radioChange = (e) => {
    setimgCount(e.target.value)
  }
  //提交表单
  const onFinish = async (value) => {
    const params = {
      ...value,
      cover: {
        type: imgcCount,
        images: fileList.map(item => item.response.data.url)
      }
    }
    await http.post('/mp/articles?draft=false', params)
    message.success('发布成功')
  }
  return (
    <div className="publish">
      <Card
        title={
          <Breadcrumb
            items={[
              {
                title: <Link to="/home">首页</Link>
              },
              {
                title: '发布文章'
              }
            ]}
          >

          </Breadcrumb>
        }
      >
        <Form
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 16 }}
          initialValues={{ type: 1, content: '请输入相关内容' }}
          onFinish={onFinish}
        >
          <Form.Item
            label="标题"
            name="title"
            rules={[{ required: true, message: '请输入文章标题' }]}
            style={{ textAlign: 'left' }}
          >
            <Input placeholder="请输入文章标题" style={{ width: 400 }} />
          </Form.Item>
          <Form.Item
            label="频道"
            name="channel_id"
            rules={[{ required: true, message: '请选择文章频道' }]}
            style={{ textAlign: 'left' }}
          >
            <Select placeholder="请选择文章频道" style={{ width: 400 }}>
              {channelStore.channelList.map(item => <Option key={item.id} value={item.id}>{item.name}</Option>)}

            </Select>
          </Form.Item>

          <Form.Item label="封面" style={{ textAlign: 'left' }}>
            <Form.Item name="type">
              <Radio.Group
                onChange={radioChange}>
                <Radio value={1}>单图</Radio>
                <Radio value={3}>三图</Radio>
                <Radio value={0}>无图</Radio>
              </Radio.Group>
            </Form.Item>
            {imgcCount > 0 && (
              <Upload
                name="image"
                listType="picture-card"
                className="avatar-uploader"
                showUploadList
                action="http://geek.itheima.net/v1_0/upload"
                fileList={fileList}
                onChange={onUploadChange}
                multiple={imgcCount > 1 ? true : false}
                maxCount={imgcCount}
              >
                <div style={{ marginTop: 8 }}>
                  <PlusOutlined />
                </div>
              </Upload>
            )}

          </Form.Item>
          <Form.Item
            label="内容"
            name="content"
            rules={[{ required: true, message: '请输入文章内容' }]}
          >
            <ReactQuill style={{ textAlign: 'left' }} theme='snow' ></ReactQuill>
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 10 }} style={{ textAlign: 'left' }}>
            <Space>
              <Button size="large" type="primary" htmlType="submit">
                发布文章
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default observer(Publish) 